package com.yw.android.letsgo.fragment.market;

import com.yw.android.common.base.BaseApp;
import com.yw.android.common.base.BaseObserver;
import com.yw.android.common.utils.FLOG;
import com.yw.android.common.utils.SystemUtils;
import com.yw.android.letsgo.api.PetService;
import com.yw.android.letsgo.bean.req.QueryDog;
import com.yw.android.letsgo.bean.req.QueryUser;
import com.yw.android.letsgo.bean.res.ComRes;
import com.yw.android.letsgo.bean.res.SellDogInfo;
import com.yw.android.letsgo.bean.res.UserInfo;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Response;

/**
 * ***********************************************************************
 * 项目名称： Yw_BaiduPet
 * 类描述：
 * 包名： com.yw.android.letsgo.fragment.market
 * 创建人：shanx
 * 创建时间： 2018/5/17 11:05
 * 修改人：shanx
 * 修改时间： 2018/5/17 11:05
 * 修改备注：
 * ***********************************************************************
 *
 * @version 1.0.0
 */
public class MarketPresenter extends MarketContract.Presenter {

    @Override
    public void queryPetsOnSale(final QueryDog mQueryDog, final boolean loadMore) {
        Observable.create(new ObservableOnSubscribe<SellDogInfo>() {
            @Override
            public void subscribe(ObservableEmitter<SellDogInfo> emitter) {
                try {
                    if (!SystemUtils.isNetworkAvailable(BaseApp.getAppContext())) {
                        emitter.onNext(new SellDogInfo());
                    } else {
                        Response<ComRes<SellDogInfo>> mReq = PetService.getInstance().getPetApi().queryPetsOnSale(mQueryDog).execute();
                        if (mReq != null && mReq.body() != null && mReq.body().data != null) {
                            emitter.onNext(mReq.body().data);
                        }

                        Response<ComRes<UserInfo>> mUserReq = PetService.getInstance().getPetApi().queryUserInfo(new QueryUser()).execute();
                        if (mUserReq != null && mUserReq.body() != null && mUserReq.body().data != null) {

                        }
                    }
                    emitter.onComplete();
                } catch (Throwable t) {
                    FLOG.e("[QueryDog : " + mQueryDog + "]queryPetsOnSale (Throwable)", t);
                }
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<SellDogInfo>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(SellDogInfo mSellDogInfo) {
                        mView.updatePetsOnSale(mSellDogInfo, loadMore);
                    }
                });
    }
}
